import { _decorator, Component } from 'cc';
const { ccclass } = _decorator;

@ccclass
export abstract class Singleton<T extends Component> extends Component {
    private static _instance: any = null;

    // 修正后的访问器（无泛型参数）
    public static get Instance(): any {
        return this._instance;
    }

    protected onLoad() {
        if ((this.constructor as any)._instance) {
            this.destroy();
            return;
        }
        (this.constructor as any)._instance = this;
    }

    protected onDestroy() {
        if ((this.constructor as any)._instance === this) {
            (this.constructor as any)._instance = null;
        }
    }
}